﻿
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Text;
using System.Collections.Generic;
using CodePlex.SpaceBlock.NAnt;
using NAnt.Core;
using System.Xml;
using NAnt.Core.Tasks;
using System.Diagnostics;


namespace CodePlex.SpaceBlock.Test
{
    /// <summary>
    ///This is a test class for CodePlex.SpaceBlock.NAnt.TestTask and is intended
    ///to contain all CodePlex.SpaceBlock.NAnt.TestTask Unit Tests
    ///</summary>
    [TestClass]
    public class NAntTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }
        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///A test for ExecuteTask ()
        ///</summary>
        [TestMethod]
        public void NAntTests()
        {


            ITaskRunner runner = new NAntTaskRunner(this.TestContext);

            StaticTests.SharedTests(runner);
          




        }


        private class NAntTaskRunner : ITaskRunner
        {
          
              private readonly TestContext _context;
            public NAntTaskRunner(TestContext context)
            {
                _context = context;

            }
            public bool RunTask(string taskName, params NameValue[] parameters)
            {

                
                BaseTask task = null; // new TestTask();
                if (taskName == "S3Delete")
                {
                    task = new S3DeleteTask();
                }
                if (taskName == "S3Upload")
                {
                    task = new S3UploadTask();
                }
                if (taskName == "S3Download")
                {
                    task = new S3DownloadTask();
                }

                foreach(NameValue parameter in parameters) {

                    task.SetProperty(parameter.Name, parameter.Value);
                   
                }
                

                task.Project = this.CreateMockProject();

                

                task.FailOnError = true;
                task.Verbose = true;

                try
                {
                    task.Execute();
                    return true;
                }
                catch (Exception /*ex*/)
                {
                    return false;
                }

            }

            void Project_BuildFinished(object sender, BuildEventArgs e)
            {
                Trace.WriteLine(e.ToString());
            }

            private Project CreateMockProject()
            {
                XmlDocument config = this.CreateConfigurationDocument();

                XmlDocument projectDoc = new XmlDocument();
                XmlElement project = projectDoc.CreateElement("project");
                projectDoc.AppendChild(project);

                Project lProject = new Project(projectDoc, Level.Verbose, 1, config.DocumentElement);
                lProject.MessageLogged += delegate(object sender, BuildEventArgs e)
                {
                    _context.WriteLine("{0}\t{1}", e.MessageLevel, e.Message);
                };

                return lProject;
            }

            private XmlDocument CreateConfigurationDocument()
            {
                XmlDocument config = new XmlDocument();



                XmlElement nant = config.CreateElement("nant");

                XmlElement frameworks = config.CreateElement("frameworks");

                XmlElement platform = config.CreateElement("platform");

                platform.SetAttribute("name", "win32");
                platform.SetAttribute("default", "auto");


                XmlElement platformTaskAssemblies = config.CreateElement("task-assemblies");


                XmlElement include01 = config.CreateElement("include");
                include01.SetAttribute("name", "*Tasks.dll");
                platformTaskAssemblies.AppendChild(include01);

                XmlElement include02 = config.CreateElement("include");
                include02.SetAttribute("name", "*Tests.dll");
                platformTaskAssemblies.AppendChild(include02);

                XmlElement include03 = config.CreateElement("include");
                include03.SetAttribute("name", "tasks/*.dll");
                platformTaskAssemblies.AppendChild(include03);

                XmlElement exclude01 = config.CreateElement("exclude");
                exclude01.SetAttribute("name", "NAnt.MSNetTasks.dll");
                platformTaskAssemblies.AppendChild(exclude01);

                XmlElement exclude02 = config.CreateElement("exclude");
                exclude02.SetAttribute("name", "NAnt.MSNet.Tests.dll");
                platformTaskAssemblies.AppendChild(exclude02);


                platform.AppendChild(platformTaskAssemblies);



                XmlElement framework = config.CreateElement("framework");

                framework.SetAttribute("name", "net-1.1");
                framework.SetAttribute("family", "net");
                framework.SetAttribute("version", "1.1");
                framework.SetAttribute("description", "Microsoft .NET Framework 1.1");
                framework.SetAttribute("runtimeengine", "");
                framework.SetAttribute("sdkdirectory", "${path::combine(sdkInstallRoot, 'bin')}");
                framework.SetAttribute("frameworkdirectory", "${path::combine(installRoot, 'v1.1.4322')}");
                framework.SetAttribute("frameworkassemblydirectory", "${path::combine(installRoot, 'v1.1.4322')}");
                framework.SetAttribute("clrversion", "1.1.4322");


                XmlElement frameworkTaskAssemblies = config.CreateElement("task-assemblies");


                XmlElement frameWorkInclude01 = config.CreateElement("include");
                frameWorkInclude01.SetAttribute("name", "tasks/net/*.dll");
                frameworkTaskAssemblies.AppendChild(frameWorkInclude01);

                XmlElement frameWorkInclude02 = config.CreateElement("include");
                frameWorkInclude02.SetAttribute("name", "tasks/net/1.1/**/*.dll");
                frameworkTaskAssemblies.AppendChild(frameWorkInclude02);


                XmlElement frameWorkExclude01 = config.CreateElement("exclude");
                frameWorkExclude01.SetAttribute("name", "NAnt.MSNetTasks.dll");
                frameworkTaskAssemblies.AppendChild(frameWorkExclude01);

                XmlElement frameWorkExclude02 = config.CreateElement("exclude");
                frameWorkExclude02.SetAttribute("name", "NAnt.MSNet.Tests.dll");
                frameworkTaskAssemblies.AppendChild(frameWorkExclude02);


                framework.AppendChild(frameworkTaskAssemblies);


                XmlElement project = config.CreateElement("project");


                XmlElement readRegistry01 = config.CreateElement("readregistry");
                readRegistry01.SetAttribute("property", "installRoot");
                readRegistry01.SetAttribute("key", @"SOFTWARE\Microsoft\.NETFramework\InstallRoot");
                readRegistry01.SetAttribute("hive", "LocalMachine");
                project.AppendChild(readRegistry01);

                XmlElement readRegistry02 = config.CreateElement("readregistry");
                readRegistry02.SetAttribute("property", "sdkInstallRoot");
                readRegistry02.SetAttribute("key", @"SOFTWARE\Microsoft\.NETFramework\sdkInstallRootv1.1");
                readRegistry02.SetAttribute("hive", "LocalMachine");
                readRegistry02.SetAttribute("failonerror", "false");
                project.AppendChild(readRegistry02);


                framework.AppendChild(project);

                platform.AppendChild(framework);



                frameworks.AppendChild(platform);
                nant.AppendChild(frameworks);

                return config;



            }



          
        }


      
     


       
       


    }


}
